package Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/logout")
public class LogOut extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //清理session
        HttpSession session = req.getSession(false);
        if(session!=null){
            session.invalidate();//销毁了
        }
        //清理cookie
        Cookie cookie = new Cookie("sessionID","");
        cookie.setMaxAge(0);//一定是0
        resp.addCookie(cookie);

        resp.sendRedirect("my.html");//重定向到登陆页面
    }
}
